﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class LvController : MonoBehaviour
{
    public static LvController Instance;
    public GameController gameController;
    public UIMain uiMain;
    public int currentLv = 0;
    public int currentItem, itemNeed, chance;
    public int second, threeSecond, twoSecond, currentSecond;
    public Dictionary<int, List<int>> itemDic = new Dictionary<int, List<int>>();
    public Dictionary<int, List<int>> itemAdd = new Dictionary<int, List<int>>();
    public GameObject imgFinger, imgGuide;
    public GameObject bag, pad;
    public GameObject suobag, suopad;
    public GameObject xgg, say;
    public GameObject btnEnterGame, LoginPanel;

    public Dictionary<int, string> itemNames =
        new Dictionary<int, string>()
        {
            {1, "L_BeanBag_01"},
            {2, "L_Chair_01"},
            {3, "L_Chair_03"},
            {4, "L_Fan_01"},
            {5, "M_Blender_01"},
            {6, "M_Cake_01"},
            {7, "M_Coffee01"},
            {8, "M_Curved_01"},
            {9, "M_Flowers_01"},
            {10, "M_FruitBowl_01"},
            {11, "M_Kettle_01"},
            {12, "M_Knife_01"},
            {13, "M_Laptop_02"},
            {14, "M_Microwave_01"},
            {15, "S_Bottle_01"},
            {16, "S_Cellphone_01"},
            {17, "S_Controller_01"},
            {18, "S_Headphones_01"},
            {19, "S_Jellybeans_01"},
            {20, "S_Milk_01"},
            {21, "S_Spaceship_01"},
            {22, "S_Stapler_01"},
        };

    private void Awake()
    {
        Instance = this;
        gameController = GetComponent<GameController>();
        itemDic.Add(1, new List<int>() { 2, 5 }); //两种物品
        itemDic.Add(2, new List<int>() { 2, 4, 5, 6 }); //加两种物品
        itemDic.Add(3, new List<int>() { 2, 4, 5, 6 }); //加两种箱子大中
        itemDic.Add(4, new List<int>() { 5, 6, 8, 9 }); //只有一种中号箱子加两种易碎物品
        itemDic.Add(5, new List<int>() { 3, 4, 5, 8 }); //两种箱子两种易碎物品
        itemDic.Add(6, new List<int>() { 2, 3, 6, 9, 17, 18 }); //大中小三种箱子，三种物品
        itemDic.Add(7, new List<int>() { 7, 8, 20, 17 }); //加入易泄露物品，只有两种箱子， 中 小
        itemDic.Add(8, new List<int>() { 3, 4, 7, 9, 20, 16 }); //加物品 三种箱子 16 20
        itemDic.Add(9, new List<int>() { 2, 3, 7, 10, 16, 18 }); //加物品 10 18
        itemDic.Add(10, new List<int>() { 2, 4, 9, 11, 16, 15 }); //11 15
        itemDic.Add(11, new List<int>() { 3, 4, 9, 12, 17, 19 }); //12 19
        itemDic.Add(12, new List<int>() { 2, 3, 10, 12, 18, 20 }); //18 20
        itemDic.Add(13, new List<int>() { 2, 3, 10, 13, 15, 21 }); //13 21
        itemDic.Add(14, new List<int>() { 2, 3, 12, 14, 20, 22 }); //14 22

        itemAdd.Add(1, new List<int>() { 4, 6 });
        itemAdd.Add(2, new List<int>() { 4, 6 });
        itemAdd.Add(3, new List<int>() { 8, 9 });
        itemAdd.Add(4, new List<int>() { 3, 8 });
        itemAdd.Add(5, new List<int>() { 17, 18 });
        itemAdd.Add(6, new List<int>() { 7, 20 });
        itemAdd.Add(7, new List<int>() { 5, 6 });
        itemAdd.Add(8, new List<int>() { 5, 6 });
        itemAdd.Add(9, new List<int>() { 5, 6 });
        itemAdd.Add(10, new List<int>() { 5, 6 });
        itemAdd.Add(11, new List<int>() { 5, 6 });
        itemAdd.Add(12, new List<int>() { 5, 6 });
        itemAdd.Add(13, new List<int>() { 5, 6 });
        itemAdd.Add(14, new List<int>() { 5, 6 });
    }



    private void Start()
    {
        UGUIEventListener.Get(btnEnterGame).onClick = (obj) =>
        {
            LoginPanel.SetActive(false);
            ToNextLv();
        };
    }

    public void ToNextLv()
    {
        currentLv++;
        currentItem = 0;
        itemNeed = currentLv / 2 + 2;
        chance = (int)(itemNeed * 1.5);

        second = currentLv * 3 + 20;
        currentSecond = 0;
        threeSecond = (int)(second * 0.6f);
        twoSecond = (int)(second * 0.8f);

        uiMain.SetTarget();
        gameController.StartGame();
        TriggerGuide();
        //StopAllCoroutines();
        //StartCoroutine(IECountDown());

        xgg.GetComponent<Animation>().Play("XGG_standby");
        say.SetActive(false);
    }

    public void AddPerfect(bool istrue)
    {
        if (istrue)
        {
            currentItem++;
            chance--;
            uiMain.slider.value = currentItem;
            uiMain.itemBili.text = currentItem.ToString() + "/" + itemNeed.ToString();
            uiMain.slider2.value = currentItem;
            uiMain.sliderBili.text = currentItem.ToString() + "/" + itemNeed.ToString();
            uiMain.text.text = "剩余" + chance.ToString() + "次机会";
            if (currentItem >= itemNeed)
            {
                //StopAllCoroutines();
                gameController.EndGame();
                Sequence q = DOTween.Sequence();
                q.AppendInterval(1).OnComplete(() => { uiMain.OpenVictory(); });
            }
        }
        else
        {
            chance--;
            uiMain.text.text = "剩余" + chance.ToString() + "次机会";
            if (chance <= 0)
            {
                gameController.EndGame();
                Sequence q = DOTween.Sequence();
                q.AppendInterval(1).OnComplete(() => { uiMain.OpenFailed(); });
            }
        }
    }

    IEnumerator IECountDown()
    {
        while(currentSecond < second)
        {
            yield return new WaitForSeconds(1);
            currentSecond++;
            uiMain.lastTime.text = currentSecond.ToString();
            if (currentSecond >= threeSecond)
                ;
            if (currentSecond >= twoSecond)
                ;
        }
        gameController.EndGame();
        uiMain.OpenFailed();
    }

    public void TriggerGuide()
    {
        if (currentLv < 4)
        {
            bag.SetActive(false);
            suobag.SetActive(true);
            pad.SetActive(false);
            suopad.SetActive(true);
        }
        else if (currentLv < 7)
        {
            pad.transform.localScale = Vector3.zero;
            pad.SetActive(true);
            suopad.SetActive(false);
            Sequence s = DOTween.Sequence();
            s.Append(pad.transform.DOScale(1.2f, 0.2f)).Append(pad.transform.DOScale(1, 0.8f));
            bag.SetActive(false);
            suobag.SetActive(true);
        }
        else
        {
            suobag.SetActive(false);
            suopad.SetActive(false);
            pad.transform.localScale = Vector3.zero;
            pad.SetActive(true);
            Sequence s = DOTween.Sequence();
            s.Append(pad.transform.DOScale(1.2f, 0.2f)).Append(pad.transform.DOScale(1, 0.8f));
            bag.transform.localScale = Vector3.zero;
            bag.SetActive(true);
            Sequence s1 = DOTween.Sequence();
            s1.Append(bag.transform.DOScale(1.2f, 0.2f)).Append(bag.transform.DOScale(1, 0.8f));
        }

        if (currentLv == 1)
        {
            imgFinger.transform.localPosition = new Vector3(370, -170, 0);
            imgFinger.transform.localScale = Vector3.zero;
            Sequence s = DOTween.Sequence();
            s.AppendInterval(2F).Append(imgFinger.transform.DOScale(1.2f, 0.1f)).Append(imgFinger.transform.DOScale(1, 0.5f))
                .Append(imgFinger.transform.DOLocalMove(new Vector3(-140, -230, 0), 1)).OnComplete(() =>
                {
                    imgFinger.transform.localPosition = new Vector3(370, -170, 0);
                    imgFinger.transform.localScale = new Vector3(1.2f, 1.2f, 1.2f);
                    Sequence s2 = DOTween.Sequence();
                    s2.Append(imgFinger.transform.DOScale(1, 0.5f)).Append(imgFinger.transform.DOLocalMove(new Vector3(-140, -230, 0), 1)).OnComplete(() =>
                    {
                        imgFinger.SetActive(false);
                    });
                });
        }
        else if (currentLv == 4)
        {
            imgGuide.SetActive(true);
            imgGuide.transform.localPosition = new Vector3(-985, 0, 0);
            Sequence s1 = DOTween.Sequence();
            s1.Append(imgGuide.transform.DOLocalMoveX(-300, 0.5F)).AppendInterval(2.5F).OnComplete(() => { imgGuide.SetActive(false); });
            imgFinger.SetActive(true);
            imgFinger.transform.localPosition = new Vector3(-365, -680, 0);
            imgFinger.transform.localScale = new Vector3(1.2f, 1.2f, 1.2f);
            Sequence s = DOTween.Sequence();
            s.Append(imgFinger.transform.DOScale(1, 0.5f)).Append(imgFinger.transform.DOLocalMove(new Vector3(-140, -230, 0), 1)).OnComplete(() =>
            {
                imgFinger.transform.localPosition = new Vector3(-365, -680, 0);
                imgFinger.transform.localScale = new Vector3(1.2f, 1.2f, 1.2f);
                Sequence s2 = DOTween.Sequence();
                s2.Append(imgFinger.transform.DOScale(1, 0.5f)).Append(imgFinger.transform.DOLocalMove(new Vector3(-140, -230, 0), 1)).OnComplete(() =>
                {
                    imgFinger.SetActive(false);
                });
            });
        }
        else
        {
            imgFinger.SetActive(false);
            imgGuide.SetActive(false);
        }
    }


}